var express = require("express");
var router = express.Router();

let item = [
  {
    key: 1,
    name: "管理员",
  },
  {
    key: 2,
    name: "vip",
  },
  {
    key: 3,
    name: "boss",
  },
];

/* GET users listing. */
router.get("/list", function (req, res) {
  res.send(item);
});
router.post("/del", function (req, res) {
  item = item.filter((i) => {
    return i.key !== req.body["key"];
  });

  res.send({ ok: 1 });
});
router.post("/add", function (req, res) {
  item.push({
    key: item.length + 1,
    name: req.body["username"],
  });
  res.send({ ok: 1 });
});
router.get("/getOne", function (req, res) {
  let key = req.query["key"];
  const data = item.find((i) => {
    return i.key == key;
  });

  res.send(data);
});
router.post("/update", function (req, res) {
  item = item.map((item) => {
    if (item.key == req.body["key"]) {
      console.log(req.body["username"]);
      return { ...item, name: req.body["username"] };
    } else {
      return item;
    }
  });
  res.send({ ok: 1 });
});
module.exports = router;
